﻿@using NFine.Domain.Entity.ProductManage
@{
    ViewBag.Title = "创建新订单";
    Layout = "~/Areas/WeiXinAreas/Views/Shared/_LayoutWeui.cshtml";
}
<style>
    .weui-cells {
        font-size: 17px;
    }

    .weui-cell__hd img {
        width: 5rem;
    }

    .weui-tabbar__item {
        font-size: 1rem;
        height: 60px;
    }
</style>
<div class="weui-cells__title">商品信息</div>
<div class="weui-cells">
    @foreach (var pro in (List<ProductEntity>)ViewData["ProductData"])
    {
        <div class="weui-cell">
            <div class="weui-cell__hd">
                <img class="hd_img" src="@pro.F_ImageUrl">
            </div>
            <div class="weui-cell__bd">
                <p>@pro.F_ProductName</p>
            </div>
            <div class="weui-cell__ft">
                <div style="color: red; font-size: .9rem;">
                    ￥@pro.F_Price
                </div>
                <div class="weui-count" data-id="@pro.F_Id" data-price="@pro.F_Price">
                    <a class="weui-count__btn weui-count__decrease"></a>
                    <input class="weui-count__number" type="number" value="0">
                    <a class="weui-count__btn weui-count__increase"></a>
                </div>
            </div>
        </div>
    }
</div>
<div class="weui-cells__title">地址信息</div>

<div class="weui-cells weui-cells_form">
    <a class="weui-cell weui-cell_access" href="Address?pid=@ViewData["pid"]">
        <div class="weui-cell__hd"><img src="~/Content/M/img/pfkh.png" style="width:44px;" /></div>
        <div class="weui-cell__bd" style="padding-left:10px;">
            @if (ViewData["F_Name"] != null && ViewData["F_Tele"] != null)
            {
                <div>@ViewData["F_Name"] <span style="font-size:12px;">@ViewData["F_Tele"]</span></div>
                <p style="font-size:12px; padding-top:10px;">@ViewData["F_Add"]</p>
            }
            else
            {
                <div>设置收件地址</div>
            }

        </div>
        <div class="weui-cell__ft">
        </div>
    </a>
</div>
    <div class="weui-cells__title">备注信息</div>
    <div class="weui-cells weui-cells_form">
        <form id="form1">


            @*<div class="weui-cell">
                    <div class="weui-cell__hd"><label for="" class="weui-label">联系人</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" id="F_Consignee" name="F_Consignee" type="text" placeholder="请输入联系人姓名" value="@ViewData["UserName"] " required>
                    </div>
                    <div class="weui-cell__ft">
                        <i class="weui-icon-warn"></i>
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__hd"><label for="" class="weui-label">联系电话</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" id="F_ContactNumber" name="F_ContactNumber" type="tel" placeholder="请输入手机号" value="@ViewData["phone"]">
                    </div>
                    <div class="weui-cell__ft">
                        <i class="weui-icon-warn"></i>
                    </div>
                </div>
                <div class="weui-cell weui-cell_select weui-cell_select-after">
                        <div class="weui-cell__hd">
                            <label for="" class="weui-label">中转中心</label>
                        </div>
                        <div class="weui-cell__bd">
                            <select class="weui-select" id="F_TransferCenterId" name="F_TransferCenterId">
                                @foreach (var v in (List<NFine.Domain.Entity.SystemManage.UserEntity>)ViewData["TransferCenterList"])
                                {
                                    <option value="@v.F_Id">@v.F_RealName</option>
                                }
                            </select>
                        </div>
                    </div>*@

            @*<div class="weui-cell weui-cell_switch">
                        <div class="weui-cell__hd"><label for="" class="weui-label">积分抵扣</label></div>
                        <div class="weui-cell__bd">@ViewData["Intger"]</div>
                        <div class="weui-cell__ft">
                            <label for="F_IsPay" class="weui-switch-cp">
                                <input id="F_IsPay" name="F_IsPay" class="weui-switch-cp__input" type="checkbox">
                                <div class="weui-switch-cp__box"></div>
                            </label>
                        </div>
                    </div>
                <div class="weui-cell">
                    <div class="weui-cell__hd"><label class="weui-label">收件地址：</label></div>
                    <div class="weui-cell__bd formValue">
                        <input class="weui-input" type="text" placeholder="请输入详细地址" id="F_Address" name="F_Address" value="@ViewData["addr"]">
                    </div>
                </div>*@
            <div class="weui-cell">
                <div class="weui-cell__bd">
                    <input type="hidden" id="F_ShopId" name="F_ShopId" value="@ViewData["F_ShopId"]">
                    <textarea class="weui-textarea" id="F_Remark" name="F_Remark" placeholder="请输入备注" rows="3"></textarea>
                    @*<div class="weui-textarea-counter"><span>0</span>/200</div>*@
                </div>
            </div>
        </form>
    </div>
    <div style="height: 60px;">
    </div>
    <div class="weui-tabbar" style="position: fixed; height: 60px;">
        <div style="width: 70%; padding-left: 1rem; line-height: 20px; padding-top:10px;">
            <p>
                总金额：@*<span id="allmoney" style="color: #9c9c9c; text-decoration:line-through;"></span>*@
                <span id="realmoney" style="color: red;">￥0.00</span>
            </p>
            @*<p style="font-size:.7rem;color:#9c9c9c;">
                    @if ((bool)ViewData["FirstOrder"])
                    {
                        @Html.Raw("首单消费，全额支付");
                    }
                    else
                    {
                        @Html.Raw("复销模式开启，全场六五折");
                    }
                </p>*@

        </div>

        <div class="weui-flex__item" style="text-align: center;  line-height: 60px; background: red; color: white;" onclick="submitForm()">
            下单
        </div>
        @*<div class="weui-tabbar__item"><p>总金额：<span>￥1100.00</span></p></div>
            <div class="weui-tabbar__item">下单</div>*@
    </div>

    @section footScript
{
        <script src="~/Content/js/framework-ui.js"></script>
        <script type="text/javascript">

            var MAX = 99, MIN = 0;
            var allmoney = 0.0, realmoney = 0.0;
            var json = new Array();
            var forder = @ViewData["topman"];
            var curMoney = @ViewData["curMoney"];
            var isinit=@ViewData["isinit"];

            $(function() {
                $('.weui-count__decrease').click(function(e) {
                    var $input = $(e.currentTarget).parent().find('.weui-count__number');
                    var number = parseInt($input.val() || "0") - 1;
                    if (number < MIN)
                        number = MIN;
                    $input.val(number);
                    updateAllMoney();
                });
                $('.weui-count__increase').click(function(e) {
                    var $weuicount = $(e.currentTarget).parent();
                    var $input = $weuicount.find('.weui-count__number');
                    var number = parseInt($input.val() || "0") + 1;
                    if (number > MAX) number = MAX;
                    $input.val(number);
                    updateAllMoney();
                });
                $('.weui-count__increase').click();
            });

            function updateAllMoney() {
                allmoney = 0.0;
                json = new Array();
                $(".weui-count").each(function() {
                    var price = parseFloat($(this).attr("data-price"));
                    var number = parseInt($(this).find('.weui-count__number').val() || "0");
                    if (number > 0) {
                        allmoney += price * number;
                        var cur = $(this).parent().parent();
                        var proId = $(this).attr("data-id"); //商品id
                        var proImg = cur.find(".hd_img").attr("src"); //商品img
                        var proName = cur.find(".weui-cell__bd").text(); //商品name
                        //var num = number;//商品数量
                        //var price = price;//商品单价
                        json.push({
                            "F_ProductId": proId,
                            "F_ProductName": proName,
                            "F_ThumbnailsUrl": proImg,
                            "F_Quantity": number,
                            "F_SellPrice": price,
                            "F_TotalPrice": number * price
                        });
                    }
                });
                realmoney = allmoney;

                if (forder) {
                    //$("#allmoney").text("￥" + allmoney.toFixed());
                    //realmoney = allmoney * 0.65;
                    $("#realmoney").text("￥" + realmoney.toFixed()+"+押金￥2000");
                }
                else
                    $("#realmoney").text("￥" + realmoney.toFixed());
            }

            function submitForm() {
                if (json.length == 0) {
                    $.alert("请选择商品！");
                    return false;
                } 
                else if($("#F_ShopId").val().length==0){
                    $.alert("请设置收货地址！")
                    return false;
                }
                //else if ($("#F_Consignee").val().length == 0) {
                //    $("#F_Consignee").parent().parent().addClass("weui-cell_warn");
                //    return false;
                //}else if($("#F_Address").val().length==0)
                //{
                //    $.toptip('请输入地址', 'error');
                //    $("#F_Address").parent().parent().addClass("weui-cell_warn");
                //    return false;
                //}
                //else {
                //    var reMobile = /^1\d{10}$/;
                //    if (!reMobile.test($("#F_ContactNumber").val())) {
                //        $("#F_ContactNumber").parent().parent().addClass("weui-cell_warn");
                //        return false;
                //    }
                //}
                // 判断积分抵用是否满足
                //if ($('#F_IsPay').prop('checked')) {
                //    if (Intger < realmoney) {
                //        $.alert("积分余额不足，不可抵扣！");
                //        return false;
                //    }

                //}


                var strJson = JSON.stringify(json);
                strJson = encodeURI(strJson);
                var postData = $("#form1").formSerialize();
                postData["jsonData"] = strJson;

                if(isinit==0){
                    $.confirm("是否将信息设置为个人基础信息？", function() {
                        postData["isinit"]=1;
                        submit(postData);
                    },function(){
                        submit(postData);
                    });
                }
                else{
                    submit(postData);
                }

            }
            var payway=0;
            function submit(postData){
                if(curMoney>=realmoney){
                    $.actions({
                        title:"请选择支付方式",
                        actions: [{
                            text:  "余额支付 <span style='color:green;font-size:12px;'>￥"+curMoney+"</span>",
                            onClick: function() {
                                postData["payway"]=payway=1;
                                submitend(postData);
                            }
                        },{
                            text: "微信支付",
                            onClick: function() {
                                submitend(postData);
                            }
                        }]
                    });
                    //$.modal({
                    //    text: "请选择支付方式：",
                    //    buttons: [
                    //      { text: "余额支付 ￥"+curMoney, onClick: function(){
                    //          postData["payway"]=payway=1;
                    //          submitend(postData);
                    //      } },
                    //      { text: "微信支付", onClick: function(){
                    //          submitend(postData);
                    //      } }
                    //    ]
                    //});
                }
                else
                {
                    submitend(postData);
                }
            };
            function submitend(postData){
                $.showLoading();
                $.ajax({
                    url: "SubmitOrder",
                    data: postData,
                    dataType: 'json',
                    type: 'post',
                    success: function (_data) {
                        $.hideLoading();
                        var res =_data;
                        if (res.state == "success") {
                            if(payway==0){
                                res = JSON.parse(res.message);
                                WeixinJSBridge.invoke(
                                       'getBrandWCPayRequest',
                                       res.wx,
                                       function (res1) {
                                           //$.toast(res1.err_msg);
                                           // $("#Remark").val(res1.err_msg);
                                           if (res1.err_msg == "get_brand_wcpay_request:ok") {
                                               // console.log('前端返回成功支付');
                                               //$.toast("支付操作成功！", "success", function () {
                                               //    window.location.href = "Ponser";
                                               //});

                                               $.modal({
                                                   text: "恭喜您成为止滑者"+"区域代表",
                                                   buttons: [
                                                     { text: "查看授权", onClick: function(){ window.location.href="shouquan"} },
                                                     { text: "个人中心", onClick: function(){ window.location.replace("Ponser")} }
                                                   ]
                                               });
                                           }
                                           else {
                                               $.toast("支付取消", "error");
                                           }
                                       }
                                   );
                            }
                            else
                            {
                                $.alert("支付成功，将前往个人中心", function() {
                                    //点击确认后的回调函数
                                    window.location.replace("Ponser");
                                });
                            }
                        }
                        else{
                            $.toast(res.message,"error");
                        }
                    },
                    complete: function () {
                    }
                });
            }

        </script>
    }
